e4625f3ffb4ed4ed4b35d22c4eb02bb984367aac
[lhc/web/wiklou.git] / includes / media / ExifBitmap.php
1 <?php
2 /**
3 * Handler for bitmap images with exif metadata.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Media
22 */
23
24 /**
25 * Stuff specific to JPEG and (built-in) TIFF handler.
26 * All metadata related, since both JPEG and TIFF support Exif.
27 *
28 * @ingroup Media
29 */
30 class ExifBitmapHandler extends BitmapHandler {
31
32 const BROKEN_FILE = '-1'; // error extracting metadata
33 const OLD_BROKEN_FILE = '0'; // outdated error extracting metadata.
34
35 function convertMetadataVersion( $metadata, $version = 1 ) {
36 // basically flattens arrays.
37 $version = explode( ';', $version, 2 );
38 $version = intval( $version[0] );
39 if ( $version < 1 || $version >= 2 ) {
40 return $metadata;
41 }
42
43 $avoidHtml = true;
44
45 if ( !is_array( $metadata ) ) {
46 $metadata = unserialize( $metadata );
47 }
48 if ( !isset( $metadata['MEDIAWIKI_EXIF_VERSION'] ) || $metadata['MEDIAWIKI_EXIF_VERSION'] != 2 ) {
49 return $metadata;
50 }
51
52 // Treat Software as a special case because in can contain
53 // an array of (SoftwareName, Version).
54 if ( isset( $metadata['Software'] )
55 && is_array( $metadata['Software'] )
56 && is_array( $metadata['Software'][0] )
57 && isset( $metadata['Software'][0][0] )
58 && isset( $metadata['Software'][0][1] )
59 ) {
60 $metadata['Software'] = $metadata['Software'][0][0] . ' (Version '
61 . $metadata['Software'][0][1] . ')';
62 }
63
64 // ContactInfo also has to be dealt with specially
65 if ( isset( $metadata['Contact'] ) ) {
66 $metadata['Contact'] =
67 FormatMetadata::collapseContactInfo(
68 $metadata['Contact'] );
69 }
70
71 foreach ( $metadata as &$val ) {
72 if ( is_array( $val ) ) {
73 $val = FormatMetadata::flattenArray( $val, 'ul', $avoidHtml );
74 }
75 }
76 $metadata['MEDIAWIKI_EXIF_VERSION'] = 1;
77 return $metadata;
78 }
79
80 function isMetadataValid( $image, $metadata ) {
81 global $wgShowEXIF;
82 if ( !$wgShowEXIF ) {
83 # Metadata disabled and so an empty field is expected
84 return self::METADATA_GOOD;
85 }
86 if ( $metadata === self::OLD_BROKEN_FILE ) {
87 # Old special value indicating that there is no Exif data in the file.
88 # or that there was an error well extracting the metadata.
89 wfDebug( __METHOD__ . ": back-compat version\n" );
90 return self::METADATA_COMPATIBLE;
91 }
92 if ( $metadata === self::BROKEN_FILE ) {
93 return self::METADATA_GOOD;
94 }
95 wfSuppressWarnings();
96 $exif = unserialize( $metadata );
97 wfRestoreWarnings();
98 if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) ||
99 $exif['MEDIAWIKI_EXIF_VERSION'] != Exif::version() )
100 {
101 if ( isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) &&
102 $exif['MEDIAWIKI_EXIF_VERSION'] == 1 )
103 {
104 //back-compatible but old
105 wfDebug( __METHOD__ . ": back-compat version\n" );
106 return self::METADATA_COMPATIBLE;
107 }
108 # Wrong (non-compatible) version
109 wfDebug( __METHOD__ . ": wrong version\n" );
110 return self::METADATA_BAD;
111 }
112 return self::METADATA_GOOD;
113 }
114
115 /**
116 * @param $image File
117 * @return array|bool
118 */
119 function formatMetadata( $image ) {
120 $meta = $this->getCommonMetaArray( $image );
121 if ( count( $meta ) === 0 ) {
122 return false;
123 }
124
125 return $this->formatMetadataHelper( $meta );
126 }
127
128 public function getCommonMetaArray( File $file ) {
129 $metadata = $file->getMetadata();
130 if ( $metadata === self::OLD_BROKEN_FILE ||
131 $metadata === self::BROKEN_FILE ||
132 $this->isMetadataValid( $file, $metadata ) === self::METADATA_BAD )
133 {
134 // So we don't try and display metadata from PagedTiffHandler
135 // for example when using InstantCommons.
136 return array();
137 }
138
139 $exif = unserialize( $metadata );
140 if ( !$exif ) {
141 return array();
142 }
143 unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
144
145 return $exif;
146 }
147
148 function getMetadataType( $image ) {
149 return 'exif';
150 }
151
152 /**
153 * Wrapper for base classes ImageHandler::getImageSize() that checks for
154 * rotation reported from metadata and swaps the sizes to match.
155 *
156 * @param File $image
157 * @param string $path
158 * @return array
159 */
160 function getImageSize( $image, $path ) {
161 global $wgEnableAutoRotation;
162 $gis = parent::getImageSize( $image, $path );
163
164 // Don't just call $image->getMetadata(); FSFile::getPropsFromPath() calls us with a bogus object.
165 // This may mean we read EXIF data twice on initial upload.
166 if ( $wgEnableAutoRotation ) {
167 $meta = $this->getMetadata( $image, $path );
168 $rotation = $this->getRotationForExif( $meta );
169 } else {
170 $rotation = 0;
171 }
172
173 if ( $rotation == 90 || $rotation == 270 ) {
174 $width = $gis[0];
175 $gis[0] = $gis[1];
176 $gis[1] = $width;
177 }
178 return $gis;
179 }
180
181 /**
182 * On supporting image formats, try to read out the low-level orientation
183 * of the file and return the angle that the file needs to be rotated to
184 * be viewed.
185 *
186 * This information is only useful when manipulating the original file;
187 * the width and height we normally work with is logical, and will match
188 * any produced output views.
189 *
190 * @param $file File
191 * @return int 0, 90, 180 or 270
192 */
193 public function getRotation( $file ) {
194 global $wgEnableAutoRotation;
195 if ( !$wgEnableAutoRotation ) {
196 return 0;
197 }
198
199 $data = $file->getMetadata();
200 return $this->getRotationForExif( $data );
201 }
202
203 /**
204 * Given a chunk of serialized Exif metadata, return the orientation as
205 * degrees of rotation.
206 *
207 * @param string $data
208 * @return int 0, 90, 180 or 270
209 * @todo FIXME orientation can include flipping as well; see if this is an
210 * issue!
211 */
212 protected function getRotationForExif( $data ) {
213 if ( !$data ) {
214 return 0;
215 }
216 wfSuppressWarnings();
217 $data = unserialize( $data );
218 wfRestoreWarnings();
219 if ( isset( $data['Orientation'] ) ) {
220 # See http://sylvana.net/jpegcrop/exif_orientation.html
221 switch ( $data['Orientation'] ) {
222 case 8:
223 return 90;
224 case 3:
225 return 180;
226 case 6:
227 return 270;
228 default:
229 return 0;
230 }
231 }
232 return 0;
233 }
234 }